'use strict'

const _ = require('lodash')
const moment = require('moment')
const Env = use('Env')
const { AliYunService } = require('../../Services')
const { FaNews, User, UserNews } = require("../../Models");
class NewsController {
  async index({request, response}) {
    let { page = 1, pageSize = 10, searchWord,year,userID } = request.all()
    let query = FaNews.query()
    if (year=='2020') {
      query.where('publishtime','<=','2020-12-31 23:59:59')
    } else if (year=='2021') {
      query.where('publishtime','>','2020-12-31 23:59:59')
    }
    let coll = await query.paginate(page, pageSize)
    coll = coll.toJSON()
    if (userID) {
      for (let i=0;i<coll.data.length;i++) {
        let isLike = 0
        let userNews = await UserNews.query().where('userID',userID).where('newsID',coll.data[i].id).first()
        if (userNews) isLike = 1
        coll.data[i].isLike = isLike
      }
    }
    response.json(coll)
  }
  async show({request, response, params}) {
    console.log('params: ', params);
    let { userID } = request.all()
    let coll = await FaNews.find(params.id)
    if (userID) {
      let isLike = 0
      let userNews = await UserNews.query().where('userID',userID).where('newsID',params.id).first()
      if (userNews) isLike = 1
      coll.isLike = isLike
    }

    response.json(coll)
  }
}
module.exports = NewsController